<template>
  <transition mode="out-in" appear
    enter-active-class="animated fadeInRight animate75"
    leave-active-class="animated fadeOutLeft animate75"
  >
    <keep-alive include="RateComponent,LoanComponent">
      <component :is="itemName" :dataArr="dataArr" @changeIndex="changeIndex"></component>
    </keep-alive>
  </transition>
</template>
<script>
import RateComponent from './components/RateComponent'
import LoanComponent from './components/LoanComponent'
import axios from 'axios'
import {httpHost} from '@/assets/requestData.js'
export default {
  name: 'rate',
  components: {
    RateComponent,
    LoanComponent
  },
  computed: {
    itemName () {
      return this.componentList[this.index].itemName
    },
    dataArr () {
      return this[this.componentList[this.index].listName]
    }
  },
  data () {
    return {
      index: 0,
      componentList: [
        {
          index: 0,
          itemName: 'RateComponent',
          listName: 'rateList'
        },
        {
          index: 1,
          itemName: 'LoanComponent',
          listName: 'loanList'
        }
      ],
      rateList: [
        // {
        //   titleZh: '人名币整存整取',
        //   titleEn: 'Time Deposit of Fixed Amount and Period',
        //   data: [
        //     {
        //       title: '三个月',
        //       content: '2.350'
        //     },
        //     {
        //       title: '半年',
        //       content: '2.351'
        //     },
        //     {
        //       title: '一年',
        //       content: '3.353'
        //     }
        //   ]
        // },
        // {
        //   titleZh: '人名币整存',
        //   titleEn: 'Time Deposit of Fixed Amount',
        //   data: [
        //     {
        //       title: '半年',
        //       content: '2.354'
        //     },
        //     {
        //       title: '一年',
        //       content: '3.355'
        //     }
        //   ]
        // },
        // {
        //   titleZh: '人名币整取',
        //   titleEn: 'Time Deposit of Fixed Period',
        //   data: [
        //     {
        //       title: '三个月',
        //       content: '1.356'
        //     },
        //     {
        //       title: '一年',
        //       content: '3.357'
        //     }
        //   ]
        // }
      ],
      loanList: [
        // {
        //   titleZh: '短期贷款',
        //   titleEn: 'Short-Time Lorn',
        //   data: [
        //     {
        //       title: '半年',
        //       content: '2.358'
        //     },
        //     {
        //       title: '一年',
        //       content: '3.359'
        //     }
        //   ]
        // },
        // {
        //   titleZh: '长期贷款',
        //   titleEn: 'Long-Time Lorn',
        //   data: [
        //     {
        //       title: '半年',
        //       content: '2.360'
        //     },
        //     {
        //       title: '一年',
        //       content: '3.361'
        //     }
        //   ]
        // }
      ],
      getDataTimer: null,
      writeDataTimer: null,
      firstVisit: true,
      zhengcun1: null,
      zhengcun2: null,
      lingcun: null,
      tongzhicun: null,
      shortBeforeLoan: null,
      shortAfterLoan: null,
      middleBeforeLoan: null,
      middleAfterLoan: null,
      houseBeforeLoan: null,
      houseAfterLoan: null,
      curData: null
    }
  },
  methods: {
    changeIndex () {
      if (this.index < this.componentList.length - 1) {
        this.index++
      } else {
        this.index = 0
      }
    },
    getDataList () {
      axios.get(httpHost + '/ibms/financial/getRate').then(this.getDataListSuccess)
      if (this.firstVisit) {
        // var this_ = this
        // setTimeout(function () {
        //   this_.getResDataFromC()
        //   this_.firstVisit = false
        //   this_.index = 0
        // }, 3000)
        this.getResDataFromC()
        this.firstVisit = false
        this.index = 0
      }
    },
    getDataListSuccess (res) {
      if (res.data) {
        var data = res.data
        if (data.flag === true) {
          window.external.WriteFile('rate', JSON.stringify(data))
          // this.curData = JSON.stringify(data)
          // 编辑rate
          // var rate = data.rate
          // this.rateList = []
          // this.addRateList(rate)
          // rate = null
          // 编辑loan
          // var loan = data.loan
          // this.loanList = []
          // this.addLoanList(loan)
          // loan = null
        }
        data = null
      }
    },
    evil (fn) {
      var Fn = Function
      return new Fn('return ' + fn)()
    },
    getResDataFromC () {
      var data = this.evil(window.external.GetJsonContextWithJsonName('rate'))
      // var data = this.evil(this.curData)
      // 编辑rate
      var rate = data.rate
      this.rateList = []
      this.addRateList(rate)
      rate = null
      // 编辑loan
      var loan = data.loan
      this.loanList = []
      this.addLoanList(loan)
      loan = null
      data = null
    },
    getCData () {
      if (this.getDataTimer) {
        clearTimeout(this.getDataTimer)
      }
      var this_ = this
      this.getDataTimer = setTimeout(function () {
        // 取得数据
        this_.getResDataFromC()
        // 继续定时启动
        this_.getCData()
      }, 50000)
    },
    saveCData () {
      if (this.writeDataTimer) {
        clearTimeout(this.writeDataTimer)
      }
      var this_ = this
      this.writeDataTimer = setTimeout(function () {
        this_.getDataList()
        this_.saveCData()
      }, 60000)
    },
    addRateList (rate) {
      this.zhengcun1 = {
        titleZh: '人民币整存',
        titleEn: 'RMB Whole Deposit',
        data: [
          {
            title: '三个月',
            content: rate.PIR_ZC_TRIMESTER
          },
          {
            title: '半年',
            content: rate.PIR_ZC_SEMIYEARLY
          },
          {
            title: '一年',
            content: rate.PIR_ZC_ONE_YEAR
          }
        ]
      }
      this.rateList.push(this.zhengcun1)
      this.zhengcun2 = {
        titleZh: '人民币整存',
        titleEn: 'RMB Whole Deposit',
        data: [
          {
            title: '二年',
            content: rate.PIR_ZC_TWO_YEAR
          },
          {
            title: '三年',
            content: rate.PIR_ZC_THREE_YEAR
          },
          {
            title: '五年',
            content: rate.PIR_ZC_FIVE_YEAR
          }
        ]
      }
      this.rateList.push(this.zhengcun2)
      this.lingcun = {
        titleZh: '人民币零存',
        titleEn: 'Lump-Sum Saving For Small Withdrawal Time Deposit',
        data: [
          {
            title: '一年',
            content: rate.PIR_LZC_ONE_YEAR
          },
          {
            title: '二年',
            content: rate.PIR_LZC_TWO_YEAR
          },
          {
            title: '五年',
            content: rate.PIR_LZC_FIVE_YEAR
          }
        ]
      }
      this.rateList.push(this.lingcun)
      this.tongzhicun = {
        titleZh: '通知存款',
        titleEn: 'Call Deposit',
        data: [
          {
            title: '一天',
            content: rate.PIR_INFORM_FUND_ONE_DAY
          },
          {
            title: '七天',
            content: rate.PIR_INFORM_FUND_SEVEN_DAY
          }
        ]
      }
      this.rateList.push(this.tongzhicun)
    },
    addLoanList (loan) {
      this.shortBeforeLoan = {
        titleZh: '短期贷款调整前利率',
        titleEn: 'Interest Rate Before Short-term Loan Adjustment',
        data: [
          {
            title: '六个月',
            content: loan.PRF_DQ_SIX_DAY_FRONT
          },
          {
            title: '一年',
            content: loan.PRF_DQ_ONE_YEAR_FUND
          }
        ]
      }
      this.loanList.push(this.shortBeforeLoan)
      this.shortAfterLoan = {
        titleZh: '短期贷款调整后利率',
        titleEn: 'Interest Rate After Short-term Loan Adjustment',
        data: [
          {
            title: '六个月',
            content: loan.PRF_DQ_SIX_DAY_BACK
          },
          {
            title: '一年',
            content: loan.PRF_DQ_ONE_YEAR_BACK
          }
        ]
      }
      this.loanList.push(this.shortAfterLoan)
      this.middleBeforeLoan = {
        titleZh: ' 中长期贷款调整前利率',
        titleEn: 'Interest Rate Before The Adjustment Of Medium And Long Term Loans',
        data: [
          {
            title: '三年',
            content: loan.PRF_ZCQ_ONE_THREE_FRONT
          },
          {
            title: '五年',
            content: loan.PRF_ZCQ_THREE_FIVE_FRONT
          },
          {
            title: '五年以上',
            content: loan.PRF_ZCQ_FIVE_UPWARDS_FRONT
          }
        ]
      }
      this.loanList.push(this.middleBeforeLoan)
      this.middleAfterLoan = {
        titleZh: ' 中长期贷款调整后利率',
        titleEn: 'Interest Rate After The Adjustment Of Medium And Long Term Loans',
        data: [
          {
            title: '三年',
            content: loan.PRF_ZCQ_ONE_THREE_BACK
          },
          {
            title: '五年',
            content: loan.PRF_ZCQ_THREE_FIVE_BACK
          },
          {
            title: '五年以上',
            content: loan.PRF_ZCQ_FIVE_UPWARDS_BACK
          }
        ]
      }
      this.loanList.push(this.middleAfterLoan)
      this.houseBeforeLoan = {
        titleZh: ' 个人住房公积金调整前利率',
        titleEn: 'Interest Rate Before Adjustment Of Individual Housing Provident Fund',
        data: [
          {
            title: '五年以下',
            content: loan.PRF_ACCUMULATION_FUND_FIVE_FRONT
          },
          {
            title: '五年以上',
            content: loan.PRF_FIVE_UPWARDS_FRONT
          }
        ]
      }
      this.loanList.push(this.houseBeforeLoan)
      this.houseAfterLoan = {
        titleZh: ' 个人住房公积金调整后利率',
        titleEn: 'Interest Rate After Adjustment Of Individual Housing Provident Fund',
        data: [
          {
            title: '五年以下',
            content: loan.PRF_ACCUMULATION_FUND_FIVE_BACK
          },
          {
            title: '五年以上',
            content: loan.PRF_FIVE_UPWARDS_BACK
          }
        ]
      }
      this.loanList.push(this.houseAfterLoan)
    }
  },
  mounted () {
    this.getDataList()
    this.saveCData()
    this.getCData()
  }
}
</script>
<style lang="stylus" scoped>
</style>
